package com.intgrt.manager.ctrl;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.intgrt.manager.entity.SysRole;
import com.intgrt.manager.entity.SysUser;
import com.intgrt.manager.midentity.CombTree;
import com.intgrt.manager.service.SysRoleService;
import com.intgrt.manager.util.ProjectConstants;
import com.intgrt.manager.util.UserInfo;

@Controller
public class SysRoleCtrl {

	@Autowired
	private SysRoleService service;
	private static final String MENU_ID = "5";

	@RequestMapping(value = "/sysrole", method = { RequestMethod.GET })
	public String index(HttpServletRequest request) {
		SysUser curuser = (SysUser)request.getSession().getAttribute(ProjectConstants.SESSION_USER);
		String btnids = "";
		try {
			btnids = curuser.getMenuButtons().getString(MENU_ID);
		} catch (Exception e) {
			btnids = "";
		}
		JSONArray btnlist = service.getMenuBtns(curuser.getId(), MENU_ID, btnids);
		request.setAttribute("btnlist", btnlist);
		return "sysrole";
	}
	@ResponseBody
	@RequestMapping(value="/d/rolelist" , method={RequestMethod.POST})
	public String list(HttpServletRequest request, SysRole entity){
		entity.setOrgid(String.valueOf(UserInfo.getOrgid()));
		return service.getList(entity);
	}
	@ResponseBody
	@RequestMapping(value="/d/rolesave" , method={RequestMethod.POST})
	public String roleSave(HttpServletRequest request, SysRole entity){
		entity.setOrgid(String.valueOf(UserInfo.getOrgid()));
		return service.saveEntity(entity);
	}
	@ResponseBody
	@RequestMapping(value="/d/roleedit" , method={RequestMethod.POST})
	public String roleEdit(HttpServletRequest request, SysRole entity){
		return service.editEntity(entity);
	}
	@ResponseBody
	@RequestMapping(value="/d/roledel" , method={RequestMethod.POST})
	public String roleDel(HttpServletRequest request){
		String ids = request.getParameter("ids");
		return service.delEntity(ids);
	}
	@ResponseBody
	@RequestMapping(value="/d/getauth" , method={RequestMethod.POST})
	public JSONArray getAuth(HttpServletRequest request){
		String roleid = UserInfo.getRoleid();
		return service.getAuthTree(Integer.parseInt(roleid));
	}
	@ResponseBody
	@RequestMapping(value="/d/roleauth" , method={RequestMethod.POST})
	public List<String> roleAuth(HttpServletRequest request){
		String roleid = request.getParameter("roleid");
		return service.roleAuth(roleid);
	}
	@ResponseBody
	@RequestMapping(value="/d/saveauth" , method={RequestMethod.POST})
	public String saveAuth(HttpServletRequest request){
		String roleid = request.getParameter("id");
		String nodes = request.getParameter("nodes");
		return service.saveAuth(roleid, nodes);
	}
	@ResponseBody
	@RequestMapping(value="/d/roleComBox" , method={RequestMethod.POST})
	public List<CombTree> comBox(HttpServletRequest request){
		String pid = request.getParameter("id");
		return service.getCombTree(pid,UserInfo.getOrgid());
	}

}